<?php

namespace Base\PicsBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class DefaultController extends Controller
{

    public function indexAction()
    {
        $album_list = array();
        $album_list_json = array();

        try
        {
            $user_id = $this->container->getParameter('flickr_api.user_id');
            $api_key = $this->container->getParameter('flickr_api.api_key');

            $uri = 'http://api.flickr.com/services/rest/?method=flickr.photosets.getList&api_key=' . $api_key . '&user_id=' . $user_id . '&format=rest';
            $album_list = simplexml_load_file( $uri, "SimpleXMLElement", LIBXML_NOCDATA );

            $json = json_encode( $album_list->photosets );
            $album_list = json_decode($json, true);
            
            foreach($album_list["photoset"] as $album ){
                $album_list_json[] = array( 'attributes' => $album["@attributes"],
                                            'title' => $album["title"],
                                            'slug' => $this->slugify( $album["title"] ),
                                            'description' => $album["description"] );
            }

        }catch(Exception $e){

        }

        /* Breadcrumbs */
        $breadcrumbs = $this->get( 'white_october_breadcrumbs' );
        $breadcrumbs->addItem( 'Accueil', $this->get("router")->generate( '_homepage' ));
        $breadcrumbs->addItem( 'Galerie photos' );

        return $this->render('BasePicsBundle:Default:index.html.twig', array( 'album_list' => $album_list_json ));
    }

    public function showAction($id_album, $slug)
    {
        $photos_list = array();
        $photos_list_json = array();

        try
        {
            $user_id = $this->container->getParameter('flickr_api.user_id');
            $api_key = $this->container->getParameter('flickr_api.api_key');

            $uri = 'http://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos&photoset_id=' . $id_album . '&api_key=' . $api_key . '&user_id=' . $user_id . '&format=rest';
            $photos_list = simplexml_load_file( $uri, "SimpleXMLElement", LIBXML_NOCDATA );

            $json = json_encode( $photos_list->photoset );
            $photos_list = json_decode($json, true);

            foreach($photos_list["photo"] as $photo ){
                $photos_list_json[] =  $photo["@attributes"];
            }

        }catch(Exception $e){

        }

        try
        {
            $uri = 'http://api.flickr.com/services/rest/?method=flickr.photosets.getInfo&photoset_id=' . $id_album . '&api_key=' . $api_key . '&user_id=' . $user_id . '&format=rest';
            $album_info = simplexml_load_file( $uri, "SimpleXMLElement", LIBXML_NOCDATA );

        }catch(Exception $e){

        }


        /* Breadcrumbs */
        $breadcrumbs = $this->get( 'white_october_breadcrumbs' );
        $breadcrumbs->addItem( 'Accueil', $this->get("router")->generate( '_homepage' ));
        $breadcrumbs->addItem( 'Galerie photos', $this->get("router")->generate( '_basePicsIndex' ));
        $breadcrumbs->addItem( $album_info->photoset->title );

        return $this->render('BasePicsBundle:Default:show.html.twig', array( 'album_info' => $album_info->photoset, 'photos_list' => $photos_list_json ));
    }

    public function homeAction()
    {
        $photos_list = array();
        $photos_list_json = array();

        try
        {
            $user_id = $this->container->getParameter('flickr_api.user_id');
            $api_key = $this->container->getParameter('flickr_api.api_key');

            $uri = 'http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=' . $api_key . '&user_id=' . $user_id . '&format=rest&per_page=50&page=1';
            $photos_list = simplexml_load_file( $uri, "SimpleXMLElement", LIBXML_NOCDATA );

            $json = json_encode( $photos_list->photos );
            $photos_list = json_decode($json, true);
            
            foreach($photos_list["photo"] as $photo ){
                $photos_list_json[] =  $photo["@attributes"];
            }

        }catch(Exception $e){

        }

        shuffle( $photos_list_json );
        $photos_list_json = array_slice( $photos_list_json, 0, 7 );
            
        
        return $this->render('BasePicsBundle:Default:home.html.twig', array( 'photos_list' => $photos_list_json ));
    }

    private function slugify($text) {
        $text = preg_replace('~[^\\pL\d]+~u', '-', $text);
        $text = trim($text, '-');
        $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
        $text = strtolower($text);
        $text = preg_replace('~[^-\w]+~', '', $text);
        
        if (empty($text)) {
            return 'n-a';
        }
        return $text;
    }
}

?>